package com.atguigu.spzx;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @author MikeCoder
 * @create 2023-12-0520:35
 * @description:
 * @verson:
 */
public class test {
    public static void main(String[] args)
            throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        try {
            //创建一个Minio的客户端对象
            MinioClient minioClient =
                    MinioClient.builder()
                            //设置服务器地址
                            .endpoint("http://192.168.65.130：9001")
                            .credentials("3NNP4Bw9nZ188LmbvxRG", "IgCgQ2DwhVzLst3vGH14wVvOPHGggnUgYqcs8LHR")
                            .build();

            // 查找指定bucket是否存在
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket("spzx").build());
            if (!found) {
                // 没有找到，就创建一个
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("spzx").build());
            } else {
                System.out.println("Bucket 'spzx' already exists.");
            }

            // 上传文件到minio服务器上
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            //指定要上传到哪一个bucket
                            .bucket("spzx")
                            //上传到服务器之后的名字
                            .object("美女.jpg")
                            //本地文件路径
                            .filename("/home/user/Photos/asiaphotos.zip")
                            .build());
            System.out.println(
                    "'/home/user/Photos/asiaphotos.zip' is successfully uploaded as "
                            + "object 'asiaphotos-2015.zip' to bucket 'asiatrip'.");
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
            System.out.println("HTTP trace: " + e.httpTrace());
        }
    }
}
